package com.example.text.fg

import android.os.Bundle
import android.provider.ContactsContract.CommonDataKinds.Email.TYPE_HOME
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.text.R
import com.example.text.databinding.FragmentMallBinding
import com.example.text.mmkv.AuthMMKV
import com.example.text.ui.mall.adepter.MallHomeMultiAdapter
import com.example.text.ui.mall.base.BaseMallHomeDataFragment
import com.example.text.ui.mall.model.MallRowTags
import com.example.text.ui.mall.viewmodel.MallDataProvider
import com.example.text.ui.mall.viewmodel.MallHomeViewModel
import com.example.text.utils.GSON
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MallFragment @Inject constructor():BaseMallHomeDataFragment<MallHomeViewModel,FragmentMallBinding>(){

    override val mLayoutResId: Int
        get() =R.layout.fragment_mall


    val mViewModel: MallHomeViewModel by viewModels()

    override fun initAdapter() {
        mAdapter = MallHomeMultiAdapter(mItems, onClick = { tag ->
            when (tag) {
                MallRowTags.GOODS_RECOMMEND -> {
                }
                MallRowTags.GOODS_PART -> {
                }
            }
        }, onBookingClick = {
        })

        // 动态设置spanCount
        val spanCount = 2
        val layoutManager: GridLayoutManager = mBinding.rvMallHome.layoutManager as GridLayoutManager
        layoutManager.spanCount = spanCount
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return try {
                    if (mItems.isEmpty())
                        return 0
                    mItems[position].getSpanSize()
                } catch (e: Exception) {
                    0
                }
            }
        }
        mBinding.rvMallHome.layoutManager = layoutManager

        super.bindAdapter(mBinding.rvMallHome)
    }

    override fun initListener() {
        //super.initListener()
        initObserve()
        //attachSmartRefreshLayout(mBinding.lRefresh)

    }

    override fun initData() {
//        if (NetworkUtil.isConnected()) {
//            mBinding.viewEmptyUi.container.visibility = View.GONE
            getDataFromNet()
//        } else {
//            showErrorUI()
//        }
    }

    override fun getDataFromNet(onRefresh: Boolean) {
        super.getDataFromNet(onRefresh)

        mViewModel.cmallAdInfo(adType = "skl_app_home_banner")
       // mViewModel.goodsList(adType = "skl_app_home_goods")
        mViewModel.cMallGroupList("c_home")

    }

    private fun initObserve() {
        // 首页Banner
        mViewModel.mallBannersLiveData.observe(this) {
            invalidateList(MallDataProvider.makeMallHomeList(vm = mViewModel))
            //Log.d("aaa", GSON.toJson(it))
        }
        mViewModel.goodsCategoryLiveData.observe(this){
            invalidateList(MallDataProvider.makeMallHomeList(vm = mViewModel))
        }
//            .observe(this) {
//                invalidateList(MallDataProvider.makeMallHomeList(vm = mViewModel))
//            }
    }


}